package com.wei.shopping.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wei.shopping.dao.*;
import com.wei.shopping.entrty.PageResult;
import com.wei.shopping.entrty.QueryPageBean;
import com.wei.shopping.pojo.*;
import com.wei.shopping.service.CommService;
import com.wei.shopping.utils.KeyUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class CommServiceImpl implements CommService {

    @Resource
    private CommMapper commMapper;
    @Resource
    private NoticesMapper noticesMapper;
    @Resource
    private NewsMapper newsMapper;
    @Resource
    private UserMapper userMapper;
    @Value("${qiniuyun}")
    private String qiniuyun;
    @Resource
    private CollectMapper collectMapper;
    /**
     * 分页查询商品数据
     * @param queryPageBean
     * @param commstatus
     * @return
     */
    @Override
    public PageResult findall(QueryPageBean queryPageBean, Integer commstatus){
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page<Commodity> page = commMapper.findall(queryPageBean.getQueryString(), commstatus);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 设置商品违规
     * @param commodity
     * @return
     */
    @Override
    public void violation(Commodity commodity){
        //设置商品违规
        commMapper.violation(commodity.getCommid());
        //给用户提示，你的商品违规了
        Notices notices = new Notices();
        notices.setId(KeyUtil.genUniqueKey());
        notices.setUserid(commodity.getUserid());
        notices.setWhys("你的商品"+commodity.getCommname()+"违规了，请您检查一下");
        notices.setTpname("商品违规");
        noticesMapper.addnotices(notices);
    }

    /**
     * 通过审核
     * @param commodity
     */
    @Override
    public void pass(Commodity commodity){
        //修改商品的审核通过的状态
        commMapper.pass(commodity.getCommid());
        //通知用户商品通过审核
        Notices notices = new Notices();
        notices.setId(KeyUtil.genUniqueKey());
        notices.setUserid(commodity.getUserid());
        notices.setWhys("你的商品"+commodity.getCommname()+"审核通过了");
        notices.setTpname("审核通过");
        noticesMapper.addnotices(notices);
    }

    /**
     * 通过id查找商品
     * @param commid
     * @return
     */
    @Override
    public Commodity findById(String commid){
        Commodity byId = commMapper.findById(commid);
        return byId;
    }
    /**
     * 通过整个商品类去查找商品
     * @param commodity
     * @return
     */
    @Override
    public Commodity findByAll(Commodity commodity){
        Commodity c = commMapper.findByAll(commodity);
        return c;
    }

    /**
     * 更改commoidty表
     * @param commid
     */
    @Override
    public void addLiulan(String commid){
        commMapper.addLiulan(commid);
    }

    /**
     * 插入商品
     * @param commodity
     */
    @Override
    public void addComm(Commodity commodity){
        commodity.setImage(qiniuyun + commodity.getImage());
        commMapper.addComm(commodity);
    }

    /**
     * 插入图片
     * @param imageList
     */
    @Override
    public void addImage(String imageList,String commid){
        //imageList = imageList.substring(4);
        String[] split = imageList.split(",");
        for (String s : split) {
            //插入数据库
            CommoImages commoImages = new CommoImages();
            commoImages.setId(KeyUtil.genUniqueKey());
            commoImages.setCommid(commid);
            commoImages.setImage(qiniuyun + s);
            commMapper.addImage(commoImages);
        }
    }
    /**
     * 通过用户id查找商品
     * @param queryPageBean
     * @param commstatus
     * @param userid
     * @return
     */
    @Override
    public PageResult findAllByUserId(QueryPageBean queryPageBean,Integer commstatus,String userid){
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<Commodity> pages = commMapper.findAllByUserId(queryPageBean.getQueryString(),commstatus,userid);
        return new PageResult(pages.getTotal(),pages.getResult());
    }

    /**
     * 设置商品已售出
     * @param commid
     * @param userid
     */
    @Override
    public void solded(String commid,String userid){
        //设置商品状态为4
        commMapper.solded(commid);
        //添加通知信息
        Notices notices = new Notices();
        notices.setTpname("商品售出");
        notices.setUserid(userid);
        notices.setId(KeyUtil.genUniqueKey());
        noticesMapper.addnotices(notices);
    }

    /**
     * 删除商品
     * @param commid
     */
    @Override
    public void deletecomm(String commid){
        commMapper.deletecomm(commid);
    }

    /**
     * 商品id查找商品
     * @param commid
     * @return
     */
    @Override
    public Map findByCommid(String commid){
        Map map = new HashMap();
        //查找商品信息
        Commodity comm = commMapper.findByCommid(commid);
        //查找图片
        List<String> images = commMapper.findImagesByCommid(commid);
        map.put("comm",comm);
        map.put("imagelist",images);
        return map;
    }

    /**
     * 修改商品信息
     * @param commodity
     */
    @Override
    public void updatecomm(Commodity commodity){
        //修改商品详情图片,先删除以前的图片，在添加新的图片
        commMapper.deleteimages(commodity.getCommid());
        //修改商品信息
        commMapper.updatecomm(commodity);
        this.addImage(commodity.getImageList(),commodity.getCommid());
    }
    /**
     * 获取热门商品和最新商品
     * @return
     */
    @Override
    public Map hotcomm(){
        Map map = new HashMap();
        //查找热门商品
        List<Commodity> hotcomm = commMapper.hotcomm();
        //查找最新商品
        List<Commodity> newcomm = commMapper.newcomm();
        //查找最行公告
        List<News> newnews = newsMapper.newnews();
        map.put("hotcomm",hotcomm);
        map.put("newcomm",newcomm);
        map.put("newnews",newnews);
        return map;
    }

    /**
     * 获取商品详情图片
     * @param commid
     * @return
     */
    @Override
    public List<String> detailpic(String commid){
        return commMapper.detailpic(commid);
    }

    /**
     * 通过类别，查找条件，排列循序分页查询
     * @param queryPageBean  查询条件
     * @param category  商品类别
     * @param paixu   排列顺序
     * @param minprice  最低价
     * @param maxprice  最高价s
     * @return
     */
    @Override
    public PageResult findAllByAll(QueryPageBean queryPageBean,String category,String paixu,Integer minprice,Integer maxprice){
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<Commodity> pages = commMapper.findAllByAll(queryPageBean.getQueryString(),category,paixu,minprice,maxprice);
        return new PageResult(pages.getTotal(),pages.getResult());
    }

    /**
     * 添加反馈
     * @param feedback
     */
    @Override
    public void addfeedback(Feedback feedback){
        commMapper.addfeedback(feedback);
    }


    /**
     * 设置商品状态
     * @param commstatus
     * @param commid
     */
    @Override
    public void updatestatus(Integer commstatus,String commid){
        commMapper.updatestatus(commstatus,commid);
    }
}
